﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevUIUtils
{
    public static class ControlEx
    {
        /// <summary>
        /// 根据子控件的Name获取控件对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ctl">容器或父控件对象</param>
        /// <param name="controlName">子控件名</param>
        /// <returns></returns>
        public static T GetSubControl<T>(this Control ctl, string controlName) where T : Component
        {
            try
            {
                Type type = ctl.GetType();
                FieldInfo fieldInfo = type.GetField(controlName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase);
                if (fieldInfo != null)
                {
                    T obj = (T)fieldInfo.GetValue(ctl);
                    return obj;
                }
                else
                {
                    return default(T);
                }
            }
            catch (Exception)
            {
                return default(T);
            }
        }
        /// <summary>
        /// 将窗体显示到Panel容器中
        /// </summary>
        /// <param name="subForm">被扩展的子窗体类</param>
        /// <param name="panel">Panel容器对象</param>
        /// <param name="bIsCloseAllSubForms">是否要关闭已打开的所有子窗体</param>
        public static void ShowInPanel(this Form subForm, Panel panel, bool bIsCloseAllSubForms = false)
        {
            for (int i = panel.Controls.Count - 1; i >= 0; i--)
            {
                Form frm = panel.Controls[i] as Form;
                if (frm == null) return;
                if (bIsCloseAllSubForms && !frm.Equals(subForm))
                    frm.Close();
                else
                    frm.Hide();
            }
            subForm.TopLevel = false;
            subForm.FormBorderStyle = FormBorderStyle.None;
            subForm.Parent = panel;
            subForm.Dock = DockStyle.Fill;
            subForm.Show();
        }

        /// <summary>
        /// 弹出Tip提示信息
        /// </summary>
        /// <param name="ctl">被扩展的控件类型</param>
        /// <param name="icon">信息图标</param>
        /// <param name="title">信息标题</param>
        /// <param name="msg">信息内容</param>
        /// <param name="tipLocationModel">显示在控件的位置(0-下、1-中间)</param>
        /// <param name="showTime">信息显示时间(毫秒)，默认2000毫秒</param>
        public static ToolTip ShowTip(this Control ctl, ToolTipIcon icon, string title, string msg, int tipLocationModel = 0, int showTime = 2000)
        {
            ToolTip infoTip = new ToolTip()
            {
                ToolTipIcon = icon,
                ToolTipTitle = title,
            };
            Point tipLocation = new Point();
            switch (tipLocationModel)
            {
                default:
                    tipLocation = new Point(0, ctl.Height);
                    break;
                case 1:
                    tipLocation = new Point(ctl.Width / 2 - 32, ctl.Height / 2 - 16);
                    break;
            }
            ctl.Enter += (sender, e) => infoTip.RemoveAll();
            if (ctl.CanFocus && ctl.Enabled)
            {
                ctl.Enabled = false;
                ctl.Enabled = true;
            }
            infoTip.Show(msg, ctl, tipLocation, showTime);
            return infoTip;
        }

        /// <summary>
        /// 弹出错误信息
        /// </summary>
        /// <param name="frm">被扩展类对象</param>
        /// <param name="msg">错误消息</param>
        /// <returns></returns>
        public static DialogResult ShowErrMsg(this Form frm, string msg)
        {
            return XtraMessageBox.Show(msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// 弹出普通信息
        /// </summary>
        /// <param name="frm">被扩展类对象</param>
        /// <param name="msg">错误消息</param>
        /// <returns></returns>
        public static DialogResult ShowMsg(this Form frm, string msg)
        {
            return XtraMessageBox.Show(msg, "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// 弹出警告信息
        /// </summary>
        /// <param name="frm">被扩展类对象</param>
        /// <param name="msg">错误消息</param>
        /// <returns></returns>
        public static DialogResult ShowWarnMsg(this Form frm, string msg)
        {
            return XtraMessageBox.Show(msg, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        /// <summary>
        /// RichTextBox扩展信息显示
        /// </summary>
        /// <param name="txtCtrl">信息显示控件对象</param>
        /// <param name="msg">信息内容</param>
        /// <param name="displayColor">显示颜色</param>
        public static void DisplayMsg(this RichTextBox txtCtrl, string msg, Color displayColor)
        {
            if (!txtCtrl.IsHandleCreated) return;
            txtCtrl.Invoke(new Action(() =>
            {
                txtCtrl.Select(txtCtrl.TextLength, 0);
                txtCtrl.ScrollToCaret();
                txtCtrl.SelectionColor = displayColor;
                txtCtrl.AppendText(msg + "\r\n");
                txtCtrl.Select(txtCtrl.TextLength, 0);
                txtCtrl.ScrollToCaret();
            }));
        }

    }
}
